package com.ruoyi.common.utils;

import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * TYF 字符串 切割 匹配多个字符 以及 最后 返回的格式 工具类
 * 2022 09 04
 *
 */
@Component
public class DymicUtils {

    /**
     * 省、市、区，自动提取器
     * @param finalStr
     * @param peopleStr
     * @return
     */

    //示例1：输入 Strs="陕西省西安市秦龙银行"  target="省、市、银行"
    //输出:return "省、市、银行"
    //解释：因为需要匹配的值是省、市、银行，但是在字符串strs中匹配到了省、市、银行，所以按顺序返回，就是省、市、银行



    //示例2：输入 Strs="蓝田县XX镇希望小学" target="省、市"
    //输出:return "";
    //解释：因为需要匹配的值市省、市，该字符串Strs中没有所匹配的字


    //示例3：输入Strs="西安市人民银行有限公司" target="省、市、公司、银行";
    //输出:return "市、公司、银行";
    //解释：因为需要匹配的值市省、市、公司、银行，字符串中市，银行，公司并列，取值顺序按照target中取值
    public static String getStrings(String finalStr,String peopleStr){
        String[] split = finalStr.split("\\,|\\，|\\.|、");
        List<String> childs=new ArrayList<>();
        for(String string:split){

            if(peopleStr.contains(string)){
                childs.add(string);
            }
        }
        Object[] objects = childs.toArray();
        String string = Arrays.toString(objects);
        String replace = string.replace("[", "").replace("]", "");
        String s = replace.replaceAll(",", "、");
        return s;
    }



    /**
     * 去除字符串中所包含的空格（包括:空格(全角，半角)、制表符、换页符等）
     * @param s
     * @return
     */
    public static String removeAllBlank(String s){
        String result = "";
        if(null!=s && !"".equals(s)){
            result = s.replaceAll("[　*| *| *|//s*]*", "");
        }
        return result;
    }


    //把一个文件中的内容读取成一个String字符串
    public static String getStr(File jsonFile){
        String jsonStr = "";
        try {
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws Exception{


        String jsonData = "E:\\newDevProject\\shga_sample_750k\\case_data_index1.json";
        // 创建一个 Path 对象，表示要读取的文件路径
        Path path = Paths.get(jsonData);
        try {
            // 使用 Files 类的 readAllBytes 方法，将文件的所有字节读取到一个 byte 数组中
            byte[] bytes = Files.readAllBytes(path);
            // 使用 Charset 类的 forName 方法，指定字符编码为 UTF-8，并将 byte 数组转换为字符串
            String json = new String(bytes, Charset.forName("UTF-8"));
            // 打印输出字符串
            String[] split = json.split("\\n");
            List<String> personInfoList = Arrays.asList(split);
            for(String caseInfo:personInfoList){
               // String newStr = caseInfo.replaceAll("[\\\"\\{\\}\\[\\]]", "");
                String replaceCaseInfo = caseInfo.replaceAll("[\\{\\}\\[\\]]", "");
                String[] newJsonStr = replaceCaseInfo.split("\\\"\\,\\\"");
                List<String> jsonList = Arrays.asList(newJsonStr);
              //  StringJoiner bjContent =new StringJoiner("，");
                for(String clonm:jsonList){//切割下来的对应字段
                    //System.out.println(clonm);


                    if(clonm.indexOf("BRIEF_CASE")!=-1){  //处理报警内容
                        String[] brife_case_arrays = clonm.split("\\\"\\:\\\"");
                        List<String> brifeContents = Arrays.asList(brife_case_arrays);
                        String content ="";
                        for(int i=0;i<brifeContents.size();i++){
                            content =brifeContents.get(brifeContents.size()-1);
                            content=   content.replaceAll("rn","");
                        }
                        System.out.println("案件简要>>>>>>"+content);  //数据库对应字段case_content
                    }


                    if(clonm.indexOf("CASE_ADDRESS")!=-1){//处理案件地址
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseAddress ="";
                        for(int i=0;i<strings.size();i++){
                            caseAddress= strings.get(strings.size()-1);
                            caseAddress=      caseAddress.replaceAll("\\\"","");
                        }

                        System.out.println("案件地址>>>>>>"+caseAddress);
                    }

                    if(clonm.indexOf("BLOCK")!=-1){//处理案件街道
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseBlock ="";
                        for(int i=0;i<strings.size();i++){
                            caseBlock=strings.get(strings.size()-1);
                            caseBlock=      caseBlock.replaceAll("\\\"","");
                        }

                        System.out.println("案件街道>>>>>>"+caseBlock);
                    }
                    if(clonm.indexOf("CASE_NUMBER")!=-1){//处理案件编号
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseNumber ="";
                        for(int i=0;i<strings.size();i++){
                            caseNumber= strings.get(strings.size()-1);
                            caseNumber=      caseNumber.replaceAll("\\\"","");
                        }

                        System.out.println("案件编号>>>>>>"+caseNumber);
                    }
                    if(clonm.indexOf("CITY")!=-1){//处理案件市区
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseCity ="";
                        for(int i=0;i<strings.size();i++){
                            caseCity= strings.get(strings.size()-1);
                            caseCity=      caseCity.replaceAll("\\\"","");
                        }

                        System.out.println("案件市区>>>>>>"+caseCity);
                    }
                    if(clonm.indexOf("LOC_SOURCE")!=-1){//处理案件来源
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseSource ="";
                        for(int i=0;i<strings.size();i++){
                            caseSource= strings.get(strings.size()-1);
                            caseSource=      caseSource.replaceAll("\\\"","");
                        }

                        System.out.println("案件来源>>>>>>"+caseSource);
                    }
                    if(clonm.indexOf("NUMBER_L7")!=-1){//处理案件物品编号
                        String[] split2 = clonm.split("\\\"\\:\\\"");

                        List<String> strings = Arrays.asList(split2);
                        String caseitemNumber ="";
                        for(int i=0;i<strings.size();i++){
                            caseitemNumber= strings.get(strings.size()-1);
                            caseitemNumber=      caseitemNumber.replaceAll("\\\"","");
                        }

                        System.out.println("物品编号>>>>>>"+caseitemNumber);
                    }
                    if(clonm.indexOf("PROVINCE_L1")!=-1){//处理案件发生市区
                        String[] split2 = clonm.split("\\\"\\:\\\"");

                        List<String> strings = Arrays.asList(split2);
                        String caseProvince ="";
                        for(int i=0;i<strings.size();i++){
                            caseProvince= strings.get(strings.size()-1);
                            caseProvince=      caseProvince.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生市区>>>>>>"+caseProvince);
                    }
                    if(clonm.indexOf("STD_ADDRESS")!=-1){//处理案件发生具体地址
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseStdAddress ="";
                        for(int i=0;i<strings.size();i++){
                            caseStdAddress=strings.get(strings.size()-1);
                            caseStdAddress=      caseStdAddress.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生具体地址>>>>>>"+caseStdAddress);
                    }


                    if(clonm.indexOf("CASE_NAME")!=-1){//处理案件发生名称
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseName ="";
                        for(int i=0;i<strings.size();i++){
                            caseName= strings.get(strings.size()-1);
                            caseName=      caseName.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生名称>>>>>>"+caseName);
                    }


                    if(clonm.indexOf("BUILDING")!=-1){//处理案件发生楼层
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseBUILDING ="";
                        for(int i=0;i<strings.size();i++){
                            caseBUILDING= strings.get(strings.size()-1);
                            caseBUILDING=      caseBUILDING.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生楼层>>>>>>"+caseBUILDING);
                    }


                    if(clonm.indexOf("COUNTY_L3")!=-1){//处理案件发生区域
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseCounty ="";
                        for(int i=0;i<strings.size();i++){
                            caseCounty= strings.get(strings.size()-1);
                            caseCounty=      caseCounty.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生区域>>>>>>"+caseCounty);
                    }


                    if(clonm.indexOf("LATITUDE")!=-1){//处理案件经纬度 纬度
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseLatitude ="";
                        for(int i=0;i<strings.size();i++){
                            caseLatitude= strings.get(strings.size()-1);
                            caseLatitude=      caseLatitude.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生纬度>>>>>>"+caseLatitude);
                    }

                    if(clonm.indexOf("LONGITUDE")!=-1){//处理案件经纬度 经度
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String caseLongitude ="";
                        for(int i=0;i<strings.size();i++){
                            caseLongitude= strings.get(strings.size()-1);
                            caseLongitude=      caseLongitude.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生经度>>>>>>"+caseLongitude);
                    }


                    if(clonm.indexOf("cjdwmc")!=-1){//处理报警单位受理名称
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String cjdwmc ="";
                        for(int i=0;i<strings.size();i++){
                            cjdwmc= strings.get(strings.size()-1);
                            cjdwmc=      cjdwmc.replaceAll("\\\"","");
                        }

                        System.out.println("报警单位受理名称>>>>>>"+cjdwmc);
                    }


                    if(clonm.indexOf("org_name")!=-1){//单位名称
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String org_name ="";
                        for(int i=0;i<strings.size();i++){
                            org_name= strings.get(strings.size()-1);
                            org_name=      org_name.replaceAll("\\\"","");
                        }

                        System.out.println("单位名称>>>>>>"+org_name);
                    }


                    if(clonm.indexOf("case_address")!=-1){//发生地址
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_address ="";
                        for(int i=0;i<strings.size();i++){
                            case_address= strings.get(strings.size()-1);
                            case_address=      case_address.replaceAll("\\\"","");
                        }

                        System.out.println("发生地址>>>>>>"+case_address);
                    }


                    if(clonm.indexOf("POI_L8")!=-1){//案件物品类型，案件详细地址
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_poi_item_place ="";
                        for(int i=0;i<strings.size();i++){
                            case_poi_item_place= strings.get(strings.size()-1);
                            case_poi_item_place=      case_poi_item_place.replaceAll("\\\"","");
                        }

                        System.out.println("案件物品类型，案件详细地址>>>>>>"+case_poi_item_place);
                    }

                    if(clonm.indexOf("ROAD_L5")!=-1){//案件发生的马路街道
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_road ="";
                        for(int i=0;i<strings.size();i++){
                            case_road= strings.get(strings.size()-1);
                            case_road=      case_road.replaceAll("\\\"","");
                        }

                        System.out.println("案件发生的马路街道>>>>>>"+case_road);
                    }


                    if(clonm.indexOf("bjrxm")!=-1){//报警人姓名
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_take_name ="";
                        for(int i=0;i<strings.size();i++){
                            case_take_name= strings.get(strings.size()-1);
                            case_take_name=      case_take_name.replaceAll("\\\"","");
                        }

                        System.out.println("报警人姓名>>>>>>"+case_take_name);
                    }

                    if(clonm.indexOf("bjrlxdh")!=-1){//报警人联系电话
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String bjrlxdh ="";
                        for(int i=0;i<strings.size();i++){
                            bjrlxdh= strings.get(strings.size()-1);
                            bjrlxdh=      bjrlxdh.replaceAll("\\\"","");
                        }

                        System.out.println("报警人联系电话>>>>>>"+bjrlxdh);
                    }

                    if(clonm.indexOf("zjhm")!=-1){//报警人证件号码
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_take_idCard ="";
                        for(int i=0;i<strings.size();i++){
                            case_take_idCard= strings.get(strings.size()-1);
                            case_take_idCard=      case_take_idCard.replaceAll("\\\"","");
                        }

                        System.out.println("报警人证件号码>>>>>>"+case_take_idCard);
                    }


                    if(clonm.indexOf("case_state")!=-1 || clonm.indexOf("caseState")!=-1){//案件状态
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_state ="";
                        for(int i=0;i<strings.size();i++){
                            case_state= strings.get(strings.size()-1);
                            case_state=      case_state.replaceAll("\\\"","");
                        }

                        System.out.println("案件状态>>>>>>"+case_state); //case_type
                    }


                    if(clonm.indexOf("REPORT_TIME")!=-1){//报警时间
                        String[] split2 = clonm.split("\\\"\\:\\\"");
                        List<String> strings = Arrays.asList(split2);
                        String case_time ="";
                        for(int i=0;i<strings.size();i++){
                            case_time= strings.get(strings.size()-1);
                            case_time=      case_time.replaceAll("\\\"","");

                            boolean legalDate = isLegalDate(case_time.length(), case_time, DateUtils.YYYY_MM_DD_HH_MM_SS);
                            if(!legalDate){
                                case_time="";
                            }
                        }

                        System.out.println("报警时间>>>>>>"+case_time); //case_type
                    }
                }

                System.out.println(">>>>>>>>>>>>>>分割线>>>>>>>>>>>>>>>>>>");
            }

        } catch (IOException e) {
            // 处理异常
            e.printStackTrace();
        }



    }

    /**
     * 根据时间 和时间格式 校验是否正确
     * @param length 校验的长度
     * @param sDate 校验的日期
     * @param format 校验的格式
     * @return
     */
    public static boolean isLegalDate(int length, String sDate,String format) {
        int legalLen = length;
        if ((sDate == null) || (sDate.length() != legalLen)) {
            return false;
        }
        DateFormat formatter = new SimpleDateFormat(format);
        try {
            Date date = formatter.parse(sDate);
            return sDate.equals(formatter.format(date));
        } catch (Exception e) {
            return false;
        }
    }
    //读取JSON文件
    public static String readJsonFile(String fileName) {
        String jsonStr ="";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            //jsonStr的首字符是空格，这应该是文件本身的问题，所以去除之(埋个坑，也可能是文件读取的机制问题，以后空了再看看)
            jsonStr = jsonStr.substring(1,jsonStr.length());//左闭右开
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }







}
